#pragma once

#include "Peranzoni.h"

bool Peranzoni::Init(Register& o_register)
{
	m_name = "Peranzoni";

	REGISTER_FUNC2(Peranzoni, 1, 2);

	return true;
}

RetType Peranzoni::Problem1()
{
	RetType sum = 0;
	
	//fast way 
	// 200+333 cycle +67 ridondance cycle (or 5 or 3) 
	// if i add the 5 and the 3 i need to subctract the 15 multipliers
	for(UInt i=0; i<1000/5; ++i)
			sum+=i*5;
	for(UInt i=0; i<=1000/3; ++i)
			sum+=i*3;
	for(UInt i=0; i<=1000/15; ++i) 
			sum-=i*15;

	// fastest way (seems like a cheat...)
	// considering that the sum of 2 number taked in pairs 
	//  from the upper and the lower at the same distance like 995+0  (or 990+5)
	//  is every time the sum n
	//  ...
	//
	//(995............0)
	//995-5...........5   = 995-5+5  =995=n
	//995-10........10   = 995-10+10=995=n
	//. first half of the 200 times so 100 times
	//. (((n/5))/2) times 
	//(999............0)
	// 999-3..........3   = 999-3+3  =999=n
	// 999-6..........6   = 999-6+6  =999=n
	//.
	//. (((n/3)+1)/2) times (cause times is odd and we need to add the central one)
	// same is for the 15 multiplier that we need to delete	
	//  999 and 960 are the last %3 and the last%15 respective

	//float a=0;
	//a=995*(((1000)/5))/2;
	//a+=999*(((1000)/3)+1)/2;
	//a-=990*(((1000)/15)+1)/2; //(also this numbers of times odd)
	//sum=static_cast<int>(a);
	//
	return sum;
}

RetType Peranzoni::Problem2()
{
	// considering that 
	//	F(n)=  F(n-1)+f(n-2)
	//		 commutativity
	//      =  F(n-2)+ f(n-1)
	//					definition of f(n-1)	
	//  	         + f(n-2)+f(n-3)
	//		=2*F(n-2)+ f(n-3)
	//	F(n)=		2*F(n-2)+		f(n-3)
	//           this is even       this can be even or odd but...
	// (F(n)%2) == (f(n-3)%2)
	// f(n) have the same eveness of f(n-3) 
	// so if the first 3 number are 1,1,2 then the sequence is odd,odd,even,
	// and the algortith become
	RetType sum = 0;

	int fn_1=1;
	int fn_2=1;
	int fn=fn_1+fn_2;
	while (fn<4000000)
	{
		sum+=fn;
		fn_1=fn_2+fn; //become fn+1
		fn_2=fn+fn_1; //become fn+2
		fn	=fn_1+fn_2; //become fn+3 the even one..
	}
	return sum;
}